using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Trans.Mini.Wx;
using JlPay.Sdk.Models.Trans.Mini.Wx;

namespace JlPay.Sdk.Examples.Trans.Mini.Wx
{
    /// <summary>
    /// 交易 - Mini微信授权绑定功能使用示例
    /// </summary>
    public class AuthBindDemo
    {
        /// <summary>
        /// 构建微信授权绑定请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static AuthBindRequest BuildAuthBindRequest()
        {
            var request = new AuthBindRequest();
            
            // 设置请求参数
            request.MchId = "849584358120018";
            request.PayType = "wxpay";
            request.JsapiPath = "http://wx.com/jspay/pay/";
            request.SubAppid = "wx1234567890abcdef";
            request.MchCreateIp = "127.0.0.1";
            
            return request;
        }

        /// <summary>
        /// 微信授权绑定示例
        /// </summary>
        public static async Task AuthBindDemoAsync()
        {
            Console.WriteLine("开始执行微信授权绑定示例");

            // 使用DemoConfig创建配置对象
            var config = DemoConfig.CreateConfigWithCrypto();
            Console.WriteLine("配置初始化完成，已启用自动加密和解密功能");

            var authBindService = new AuthBindService(config);

            // 创建微信授权绑定请求
            var request = BuildAuthBindRequest();

            try
            {
                // 发送请求
                var response = await authBindService.AuthBindAsync(request);

                // 使用优化的响应处理
                response.HandleResponse("微信授权绑定", config);
            }
            catch (Exception ex)
            {
                // 使用优化的异常处理
                var response = new AuthBindResponse();
                response.HandleResponseWithException("微信授权绑定", config, ex);
            }

            Console.WriteLine("微信授权绑定示例执行完毕");
        }

        /// <summary>
        /// 主程序入口
        /// </summary>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 微信授权绑定示例 开始 =====");
            Console.WriteLine("=" + new string('=', 50));

            await AuthBindDemoAsync();

            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 微信授权绑定示例执行完毕 =====");
            Console.WriteLine("=" + new string('=', 50));
        }
    }
} 